package henu.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import henu.javaweb.bean.Teacher;
import henu.javaweb.dao.DaoFactory;
import henu.javaweb.util.MD5;


/**
 * Servlet implementation class TeacherLoginServlet
 */
@WebServlet("/TeacherLoginServlet")
public class TeacherLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;Charset=utf-8");
		PrintWriter out=response.getWriter();
        
		request.setCharacterEncoding("utf-8");
		String tid= request.getParameter("tid");
		String tpassword= request.getParameter("tpassword");
		//String MD5Password=MD5.getResult(password);
		Teacher t=DaoFactory.getTeacherDaoInstance().login(tid, tpassword);
		if(t!=null) {
			request.getSession().setAttribute("teacher",t);
			response.sendRedirect("jsp/teacher/teacherMain.jsp");
		}else {
			out.print("<script type=\"text/javascript\">alert('账号或密码有误!');history.go(-1);</script>");
			//response.sendRedirect("jsp/MainLoginPage.jsp");
		}
	}

}
